package com.example.handler;

import com.example.entity.SysEntity;
import com.example.entity.SysTask;
import com.example.service.SysTaskService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

public abstract class AbstractSysTaskHandler implements SysTaskHandler {

    Executor executor = Executors.newCachedThreadPool();

    @Resource
    private SysTaskService sysTaskService;

    @Override
    public SysTaskService getSysTaskService() {
        return sysTaskService;
    }

    public void setSysTaskService(SysTaskService sysTaskService) {
        this.sysTaskService = sysTaskService;
    }

    @Override
    public Boolean isSpeedy() {
        return Boolean.FALSE;
    }

    /*
     * 功能描述: 针对单个流程任务的分发
     *
     * @Author: chentao
     * @Date: 2023/3/24 12:25
     * @return:
     */
    @Override
    public void send(SysEntity sysEntity) {
        SysTask sysTask = new SysTask();
        sysTask.setModelName(sysEntity.getClass().getName());
        sysTask.setModelId(sysEntity.getId());
        getSysTaskService().dispatcher(sysTask);
    }

    /*
     * 功能描述: 针对大批量任务的分发
     *
     * @Author: chentao
     * @Date: 2023/3/24 12:26
     * @return:
     */
    @Override
    public void sendBatch(List list) {
        List<SysTask> sysTasks = new ArrayList<>();
        for (Object object : list) {
            SysEntity sysEntity = (SysEntity) object;
            SysTask sysTask = new SysTask();
            sysTask.setQueueType(isSpeedy());
            sysTask.setModelName(sysEntity.getClass().getName());
            sysTask.setModelId(sysEntity.getId());
            sysTasks.add(sysTask);
        }
        getSysTaskService().updateAndDispatcherListToQueue(sysTasks);
    }
}
